<?php

// [ JS接口临时票据 ]

namespace app\wechat\lib;
use think\Config;
use think\Cache;
use app\wechat\lib\AccessToken;

class JsapiTicket
{
    // https请求方式: GET
    // https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
    private static function init()
    {
        $uri = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'
             . '?access_token=' . AccessToken::get()
             . '&type=jsapi';
        $res = https_curl($uri);
        $res = json_decode($res,true);
        return $res;
    }

    public static function get()
    {
        $jsapi_ticket = Cache::get('jsapi_ticket');
        if ( !$jsapi_ticket ) {
            $res = self::init();
            Cache::set('jsapi_ticket',$res['ticket'],$res['expires_in'] - 200);
            $jsapi_ticket = $res['ticket'];
        }
        return $jsapi_ticket;
    }
}
